package com.it.travel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.it.travel.dao.CategoryMapper;
import com.it.travel.dao.CategoryProductMapper;
import com.it.travel.dao.ProductMapper;
import com.it.travel.pojo.Category;
import com.it.travel.pojo.CategoryProduct;
import com.it.travel.service.RouteService;
import com.it.travel.vo.Params.RouteParams;
import com.it.travel.vo.Result;
import com.it.travel.vo.RouteVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class RouteServiceImpl implements RouteService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private CategoryProductMapper categoryProductMapper;

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * @param routeParams catrgoryId1 旅游地分类id  catrgoryId2  出游日期分类id
     * @return
     */
    @Override
    public Result<?> getInfo(RouteParams routeParams) {
        Integer sort = routeParams.getSort();
        Integer catrgoryId1 = routeParams.getCatrgoryId1();
        Integer catrgoryId2 = routeParams.getCatrgoryId2();
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<Category> wrapper1 = Wrappers.lambdaQuery();
        wrapper1.eq(Category::getName, "地点"); // 先查出对应分类下的所有结点
        Category category = categoryMapper.selectOne(wrapper1);
        LambdaQueryWrapper<Category> wrapper2 = Wrappers.lambdaQuery();
        wrapper2.eq(Category::getParentId, category.getId());
        wrapper2.select(Category::getId, Category::getName);
        List<Category> categoryList = categoryMapper.selectList(wrapper2);
// ======================
        LambdaQueryWrapper<Category> wrapper3 = Wrappers.lambdaQuery();
        wrapper3.eq(Category::getName, "旅游日期"); // 先查出对应分类下的所有结点
        Category category1 = categoryMapper.selectOne(wrapper3);
        LambdaQueryWrapper<Category> wrapper4 = Wrappers.lambdaQuery();
        wrapper4.eq(Category::getParentId, category1.getId());
        wrapper4.select(Category::getId, Category::getName);
        List<Category> categoryList2 = categoryMapper.selectList(wrapper4);
//        ====================
//        先查出 目的地分类的id，因为要查询含有着目的地分类id的 旅游商品数据

            Category category2 = categoryMapper.selectOne(new QueryWrapper<Category>().eq("name", "目的地"));
        System.out.println("category2.getId() = " + category2.getId());  // 目的地对应的分类id
        List<RouteVO> routeVOList = new ArrayList<>();
        List<RouteVO> list = new ArrayList<>();
        Set<RouteVO> set = new HashSet<>();
        //  旅行地全部 && 旅行日期全部
        list = productMapper.queryRouteInfo(category2.getId(), catrgoryId1, catrgoryId2,sort);

        if (catrgoryId1 != 0) {
            for (RouteVO routeVO : list) { // 查询出的数据
                QueryWrapper<CategoryProduct> wrapper = new QueryWrapper<>();
                wrapper.eq("category_id", routeVO.getCategoryId());
                wrapper.eq("product_id", routeVO.getId());
                List<CategoryProduct> categoryProducts = categoryProductMapper.selectList(wrapper);// 如果查询出来的数据的分类在中间表可以查询到
                if (categoryProducts.size() > 0 && routeVO.getCategoryId() != category2.getId()) {
                    set.add(routeVO);
                }
            }
        } else {
            for (RouteVO routeVO : list) { // 查询出的数据
                QueryWrapper<CategoryProduct> wrapper = new QueryWrapper<>();
                wrapper.eq("category_id", routeVO.getCategoryId());
                wrapper.eq("product_id", routeVO.getId());
                List<CategoryProduct> categoryProducts = categoryProductMapper.selectList(wrapper);// 如果查询出来的数据的分类在中间表可以查询到
                if (categoryProducts.size() > 0) {
                    set.add(routeVO);
                }
            }
        }
        Iterator<RouteVO> iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println("iterator.next() = " + iterator.next());
        }
        map.put("categoryList", categoryList);
        map.put("categoryList2", categoryList2);
        map.put("routeVOList", set);
        return Result.success(map, "获取数据成功");
    }


}
